package com.atguigu.gmall.realtime.utils;

import org.wltea.analyzer.core.IKSegmenter;
import org.wltea.analyzer.core.Lexeme;

import java.io.IOException;
import java.io.StringReader;
import java.util.ArrayList;
import java.util.List;

/**
 * Author: Felix
 * Date: 2021/9/24
 * Desc: 关键词分词工具类
 */
public class KeywordUtil {
    public static List<String> analyze(String text){
        StringReader reader = new StringReader(text);
        //创建分词器
        IKSegmenter ikSegmenter = new IKSegmenter(reader,true);
        List<String> keywordList = new ArrayList<>();
        //分词
        Lexeme lexeme = null;
            try {
                while((lexeme = ikSegmenter.next())!=null){
                    String keyword = lexeme.getLexemeText();
                    keywordList.add(keyword);
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
            return keywordList;
    }

    public static void main(String[] args) {
        List<String> keywordList = analyze("Apple iPhoneXSMax (A2104) 256GB 深空灰色 移动联通电信4G手机 双卡双待");
        System.out.println(keywordList);
    }
}
